from django.shortcuts import render
from rest_framework import viewsets

from .models import SmartLocks, DefectiveStoreLocks, DefectiveStoreInRecord, DefectiveStoreOutRecord, DefectiveStore
from .serializers import SmartLocksSerializer, DefectiveStoreLocksSerializer, DefectiveStoreInRecordSerializer, DefectiveStoreOutRecordSerializer, DefectiveStoreSerializer
from rest_framework.decorators import permission_classes  # 导入权限装饰器
# Create your views here.
from django_filters import FilterSet, rest_framework as filters
from rest_framework.decorators import permission_classes  # 导入权限装饰器
from rest_framework import viewsets, permissions
from rest_framework.filters import SearchFilter, OrderingFilter
from utils.pagination import MyPagination  # 导入自定义的分页类
from rest_framework.schemas import AutoSchema

@permission_classes([permissions.IsAuthenticated])  # 设置权限为需要认证的用户
class BaseViewSet(viewsets.ModelViewSet):
    """
    基础视图集，用于减少重复代码。
    """
    # permission_classes = [permissions.IsAuthenticated]  # 设置权限为需要认证的用户
    # http_method_names = ['get', 'post', 'delete', 'put']  # 设置允许的HTTP方法
    # 可以添加一些通用的设置，例如权限控制、分页等
    pagination_class = MyPagination  # 设置分页类
    schema = AutoSchema()  # 设置自动生成文档
    filter_backends = [SearchFilter, OrderingFilter]  # 设置过滤后端

class SmartLocksViewSet(BaseViewSet):
    queryset = SmartLocks.objects.all()
    serializer_class = SmartLocksSerializer

class DefectiveStoreViewSet(BaseViewSet):
    queryset = DefectiveStore.objects.all()
    serializer_class = DefectiveStoreSerializer
    filter_backends = [SearchFilter, OrderingFilter]

class DefectiveStoreLocksViewSet(BaseViewSet):
    queryset = DefectiveStoreLocks.objects.all()
    serializer_class = DefectiveStoreLocksSerializer
    filter_backends = [SearchFilter, OrderingFilter]

class DefectiveStoreInRecordViewSet(BaseViewSet):
    queryset = DefectiveStoreInRecord.objects.all()
    serializer_class = DefectiveStoreInRecordSerializer
    filter_backends = [SearchFilter, OrderingFilter]

class DefectiveStoreOutRecordViewSet(BaseViewSet):
    queryset = DefectiveStoreOutRecord.objects.all()
    serializer_class = DefectiveStoreOutRecordSerializer
    filter_backends = [SearchFilter, OrderingFilter]
